import { SUBJECT_TYPE } from '@/config/enum'

export default {
    namespaced: true,
    state: {
        subjectList: [],
        subjectCartList: []
    },
    getters: {
        cartCount(state) {
            return state.subjectCartList.length
        },
        cart_danxuanlist(state) {
            return state.subjectCartList.filter(r => r.type === SUBJECT_TYPE.DANXUAN)
        },
        cart_duoxuanlist(state) {
            return state.subjectCartList.filter(r => r.type === SUBJECT_TYPE.DUOXUAN)
        },
        cart_panduanlist(state) {
            return state.subjectCartList.filter(r => r.type === SUBJECT_TYPE.PANDUAN)
        },
        cart_jiandalist(state) {
            return state.subjectCartList.filter(r => r.type === SUBJECT_TYPE.JIANDA)
        }

    },
    mutations: {
        setSubjectList(state, payload) {
            state.subjectList = payload
        },
        add2Cart(state, payload) {
            if (state.subjectCartList.some(r => r.id === payload.id)) {
                payload.isSuccess = false
            } else {
                state.subjectCartList.push(payload)
                payload.isSuccess = true
            }
        },
        removeFromCart(state, payload) {
            state.subjectCartList = state.subjectCartList.filter(r => r.id != payload)

            // let a = state.subjectCartList.indexOf(r => r.id = payload)
            // if (a > 0) {
            //     state.subjectCartList.splice(a, 1)
            // }
        }
    },
    actions: {
        test() {
            return 666
        }
    }
}